<?php
/**
 * Created by PhpStorm.
 * User: Talent
 * Date: 2018/5/16
 * Time: 17:50
 */
namespace app\admin\controller;
use think\Controller;
use veda\util\Tools;
use think\Session;
use app\admin\model\Grade;
use app\index\model\Sign;
use think\Db;
class Index extends Controller{

    public function index(){
        $user=Session::get("user");
        Tools::checkLogin($user,'0');

//        $basic=Basic::get(["uuid"=>'80a57367-b79a-26e7-3eca-627f0d70beb7']);
//        $this->assign([
//            'uuid'  => $basic->uuid,
//            'system_name' => $basic->system_name,
//            'user_name'=>$user['nickname'],
//        ]);


//        $basic=Basic::get(["uuid"=>'80a57367-b79a-26e7-3eca-627f0d70beb7']);
//        $this->assign([
//            'uuid'  => $basic->uuid,
//            'system_name' => $basic->system_name,
//            'user_name'=>$user['nickname'],
//            'edit_object'=>$basic,
//        ]);
        return $this->fetch('index');
    }
    public function getSignTable($grade=''){
        $list = '';
        $Sign=new Sign;
        if($grade=='all'){
            $list=$Sign
                ->order('create_time','ASC')
                ->select();
        }else{
            $list=$Sign
                ->where('p_class',$grade)
                ->order('create_time','ASC')
                ->select();
        }

        $array_json=array();

        foreach($list as $key=>$item){
            array_push($array_json,$item);
        }

        echo json_encode($array_json);
    }
    public function getClass(){
        $grade=new Grade;
        $list=$grade
            ->order('p_grade','ASC')
            ->select();
        $array_json=array();

        foreach($list as $key=>$item){
            array_push($array_json,$item);
        }

        echo json_encode($array_json);
    }

    public function exportExcel($report_grade=''){
        $data;
        $where_value=null;
        if($report_grade!='all'){
            $where_value=$where_value."p_class=".input('report_grade');
            $data=DB::table("sign")
                ->field(
                    '
                id,
                p_name,
                p_class,
                create_time,
                p_message
                '
                )->where($where_value)->select();
        }else{
            $data=DB::table("sign")
                ->field(
                    '
                id,
                p_name,
                p_class,
                create_time,
                p_message
                '
                )->select();
        }



        //echo DB::table("cell")->getLastSql();

        $xlsTitle='报表';
        $fileName="报表";
        vendor("Classes.PHPExcel");
        $objPHPExcel = new \PHPExcel();
        //设置当前的sheet
        $objPHPExcel->setActiveSheetIndex(0);
        //设置sheet的name
        $objPHPExcel->getActiveSheet()->setTitle('工作薄1');
        //设置单元格的值
        $objPHPExcel->getActiveSheet()->setCellValue('A1', '编号');
        $objPHPExcel->getActiveSheet()->setCellValue('B1', '学生姓名');
        $objPHPExcel->getActiveSheet()->setCellValue('C1', '学生班级');
        $objPHPExcel->getActiveSheet()->setCellValue('D1', '签到时间');
        $objPHPExcel->getActiveSheet()->setCellValue('E1', '家长寄语');

//        dump($data);
        for($i=2;$i<count($data)+2;$i++){
            $objPHPExcel->getActiveSheet()->setCellValue('A'.($i), $i-1);
            $objPHPExcel->getActiveSheet()->setCellValue('B'.($i), $data[$i-2]["p_name"]);
            $objPHPExcel->getActiveSheet()->setCellValue('C'.($i), $data[$i-2]["p_class"]);
            $objPHPExcel->getActiveSheet()->setCellValue('D'.($i), $data[$i-2]["create_time"]);
            $objPHPExcel->getActiveSheet()->setCellValue('E'.($i), $data[$i-2]["p_message"]);
        }
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');

    }
}